home *** CD-ROM | disk | FTP | other *** search
/ Aminet 48 / Aminet 48 (2002)(GTI - Schatztruhe)[!][Apr 2002].iso / Aminet / text / edit / vim60rt.lha / Vim / vim60 / doc / map.txt < prev    next >
Encoding:
Text File  |  2001-09-26  |  41.7 KB  |  1,022 lines

  1. *map.txt*       For Vim version 6.0.  Last change: 2001 Sep 26
  2.  
  3.  
  4.           VIM REFERENCE MANUAL    by Bram Moolenaar
  5.  
  6.  
  7. Key mapping, abbreviations and user-defined commands.
  8.  
  9. This subject is introduced in sections |05.3|, |24.7| and |40.1| of the user
  10. manual.
  11.  
  12. 1. Key mapping            |key-mapping|
  13. 2. Abbreviations        |abbreviations|
  14. 3. Local mappings and functions    |script-local|
  15. 4. User-defined commands    |user-commands|
  16.  
  17. ==============================================================================
  18. 1. Key mapping                *key-mapping* *mapping* *macro*
  19.  
  20. Key mapping is used to change the meaning of typed keys.  The most common use
  21. is to define a sequence commands for a function key.  Example: >
  22.  
  23.     :map <F2> a<C-R>=strftime("%c")<CR><Esc>
  24.  
  25. This appends the current date and time after the cursor. (in <> notation |<>|)
  26.  
  27. There are commands to enter new mappings, remove mappings and list mappings.
  28. See |map-overview| for the various forms of "map" and their relationships with
  29. modes.
  30.  
  31. {lhs}    means left-hand-side    *{lhs}*
  32. {rhs}    means right-hand-side    *{rhs}*
  33.  
  34. :map    {lhs} {rhs}                    *:map*
  35. :nm[ap]    {lhs} {rhs}                    *:nm* *:nmap*
  36. :vm[ap]    {lhs} {rhs}                    *:vm* *:vmap*
  37. :om[ap]    {lhs} {rhs}                    *:om* *:omap*
  38. :map!    {lhs} {rhs}                    *:map!*
  39. :im[ap]    {lhs} {rhs}                    *:im* *:imap*
  40. :lm[ap]    {lhs} {rhs}                    *:lm* *:lmap*
  41. :cm[ap]    {lhs} {rhs}                    *:cm* *:cmap*
  42.             Map the key sequence {lhs} to {rhs} for the modes
  43.             where the map command applies.  The result, including
  44.             {rhs}, is then further scanned for mappings.  This
  45.             allows for nested and recursive use of mappings.
  46.  
  47.  
  48. :no[remap]  {lhs} {rhs}                    *:no*  *:noremap*
  49. :nn[oremap] {lhs} {rhs}                    *:nn*  *:nnoremap*
  50. :vn[oremap] {lhs} {rhs}                    *:vn*  *:vnoremap*
  51. :ono[remap] {lhs} {rhs}                    *:ono* *:onoremap*
  52. :no[remap]! {lhs} {rhs}                    *:no!* *:noremap!*
  53. :ino[remap] {lhs} {rhs}                    *:ino* *:inoremap*
  54. :lno[remap] {lhs} {rhs}                    *:ln*  *:lnoremap*
  55. :cno[remap] {lhs} {rhs}                    *:cno* *:cnoremap*
  56.             Map the key sequence {lhs} to {rhs} for the modes
  57.             where the map command applies.  Disallow mapping of
  58.             {rhs}, to avoid nested and recursive mappings.  Often
  59.             used to redefine a command.  {not in Vi}
  60.  
  61.  
  62. :unm[ap]  {lhs}                        *:unm*  *:unmap*
  63. :nun[map] {lhs}                        *:nun*  *:nunmap*
  64. :vu[nmap] {lhs}                        *:vu*   *:vunmap*
  65. :ou[nmap] {lhs}                        *:ou*   *:ounmap*
  66. :unm[ap]! {lhs}                        *:unm!* *:unmap!*
  67. :iu[nmap] {lhs}                        *:iu*   *:iunmap*
  68. :lu[nmap] {lhs}                        *:lu*   *:lunmap*
  69. :cu[nmap] {lhs}                        *:cu*   *:cunmap*
  70.             Remove the mapping of {lhs} for the modes where the
  71.             map command applies.  The mapping may remain defined
  72.             for other modes where it applies.
  73.             Note: Trailing spaces are included in the {lhs}.  This
  74.             unmap does NOT work: >
  75.                 :map @@ foo
  76.                 :unmap @@ | print
  77.  
  78. :mapc[lear]                        *:mapc*   *:mapclear*
  79. :nmapc[lear]                        *:nmapc*  *:nmapclear*
  80. :vmapc[lear]                        *:vmapc*  *:vmapclear*
  81. :omapc[lear]                        *:omapc*  *:omapclear*
  82. :mapc[lear]!                        *:mapc!*  *:mapclear!*
  83. :imapc[lear]                        *:imapc*  *:imapclear*
  84. :lmapc[lear]                        *:lmapc*  *:lmapclear*
  85. :cmapc[lear]                        *:cmapc*  *:cmapclear*
  86.             Remove ALL mappings for the modes where the map
  87.             command applies.  {not in Vi}
  88.             Warning: This also removes the default mappings.
  89.  
  90. :map
  91. :nm[ap]
  92. :vm[ap]
  93. :om[ap]
  94. :map!
  95. :im[ap]
  96. :lm[ap]
  97. :cm[ap]
  98.             List all key mappings for the modes where the map
  99.             command applies.  Note that ":map" and ":map!" are
  100.             used most often, because they include the other modes.
  101.  
  102. :map    {lhs}                        *:map_l*
  103. :nm[ap] {lhs}                        *:nmap_l*
  104. :vm[ap] {lhs}                        *:vmap_l*
  105. :om[ap] {lhs}                        *:omap_l*
  106. :map!   {lhs}                        *:map_l!*
  107. :im[ap] {lhs}                        *:imap_l*
  108. :lm[ap] {lhs}                        *:lmap_l*
  109. :cm[ap] {lhs}                        *:cmap_l*
  110.             List the key mappings for the key sequences starting
  111.             with {lhs} in the modes where the map command applies.
  112.             {not in Vi}
  113.  
  114. These commands are used to map a key or key sequence to a string of
  115. characters.  You can use this to put command sequences under function keys,
  116. translate one key into another, etc.  See |:mkexrc| for how to save and
  117. restore the current mappings.
  118.  
  119.                 *:map-local* *:map-<buffer>* *E224* *E225*
  120. If the first argument to one of these commands is "<buffer>" it will apply to
  121. mappings locally to the current buffer only.  Example: >
  122.     :map <buffer>  ,w  /[.,;]<CR>
  123. Then you can map ",w" to something else in another buffer: >
  124.     :map <buffer>  ,w  /[#&!]<CR>
  125. The local buffer mappings are used before the global ones.
  126.  
  127.                         *:map-<silent>* *:map-silent*
  128. To define a mapping which will not be echoed on the command line, add
  129. "<silent>" as the first argument.  Example: >
  130.     :map <silent> ,h /Header<CR>
  131. The search string will not be echoed when using this mapping.  Messages from
  132. the executed command are still given though.  To shut them up too, add a
  133. ":silent" in the executed command: >
  134.     :map <silent> ,h :exe ":silent normal /Header\r"<CR>
  135. Prompts will still be given, e.g., for inputdialog().
  136.  
  137.                         *:map-<script>* *:map-script*
  138. If the first argument to one of these commands is "<script>" and it is used to
  139. define a new mapping or abbreviation, the mapping will only remap characters
  140. in the {rhs} using mappings that were defined local to a script, starting with
  141. "<SID>".  This can be used to avoid that mappings from outside a script
  142. interfere (e.g., when CTRL-V is remapped in mswin.vim), but do use other
  143. mappings defined in the script.
  144. Note: ":map <script>" and ":noremap <script>" do the same thing.  The
  145. "<script>" overrules the command name.  Using ":noremap <script>" is
  146. preferred, because it's clearer that remapping is (mostly) disabled.
  147.  
  148.                         *:map-<unique>* *E226* *E227*
  149. If the first argument to one of these commands is "<unique>" and it is used to
  150. define a new mapping or abbreviation, the command will fail if the mapping or
  151. abbreviation already exists.  Example: >
  152.     :map <unique> ,w  /[#&!]<CR>
  153. When defining a local mapping, there will also be a check if a global map
  154. already exists which is equal.
  155. Example of what will fail: >
  156.     :map ,w  /[#&!]<CR>
  157.     :map <buffer> <unique> ,w  /[.,;]<CR>
  158.  
  159. "<buffer>", "<silent>", "<script>" and "<unique>" can be used in any order.
  160. They must appear right after the command, before any other arguments.
  161.  
  162.  
  163. MAPPING AND MODES
  164.  
  165. There are five sets of mappings
  166. - For Normal mode: When typing commands.
  167. - For Visual mode: When typing commands while the Visual area is highlighted.
  168. - For Operator-pending mode: When an operator is pending (after "d", "y", "c",
  169.   etc.).  Example: ":omap { w" makes "y{" work like "yw" ad "d{" like "dw".
  170. - For Insert mode. These are also used in Replace mode.
  171. - For Command-line mode: When entering a ":" or "/" command.
  172.  
  173. There are no separate mappings for Select mode.  The same as for Visual mode
  174. are used |Select-mode-mapping|.
  175.  
  176.                         *map-overview* *map-modes*
  177. Overview of which map command works in which mode:
  178.  
  179.     commands:                      modes: ~
  180.                       Normal     Visual  Operator-pending ~
  181. :map   :noremap   :unmap   :mapclear        yes           yes      yes
  182. :nmap  :nnoremap  :nunmap  :nmapclear        yes        -       -
  183. :vmap  :vnoremap  :vunmap  :vmapclear         -           yes       -
  184. :omap  :onoremap  :ounmap  :omapclear         -        -      yes
  185.  
  186.                       Insert  Command-line    Lang-Arg ~
  187. :map!  :noremap!  :unmap!  :mapclear!        yes           yes       -
  188. :imap  :inoremap  :iunmap  :imapclear        yes        -       -
  189. :cmap  :cnoremap  :cunmap  :cmapclear         -           yes       -
  190. :lmap  :lnoremap  :lunmap  :lmapclear        yes*       yes*      yes*
  191.  
  192. The original Vi did not have separate mappings for
  193. Normal/Visual/Operator-pending mode and for Insert/Command-line mode.
  194. Therefore the ":map" and ":map!" commands enter and display mappings for
  195. several modes.  In Vim you can use the ":nmap", ":vmap", ":omap", ":cmap" and
  196. ":imap" commands to enter mappings for each mode separately.
  197.  
  198. To enter a mapping for Normal and Visual mode, but not Operator-pending mode,
  199. first define it for all three modes, then unmap it for Operator-pending mode:
  200.     :map    xx something-difficult
  201.     :ounmap xx
  202. Likewise for a mapping for Visual and Operator-pending mode or Normal and
  203. Operator-pending mode.
  204.  
  205.                         *language-mapping*
  206. ":lmap" defines a mapping that applies to Insert and Command-line mode, the
  207. argument of the commands that accept a text character, when entering a search
  208. pattern and for the input() line.  Generally: Whenever a character is to be
  209. typed that is inserted in the text, not a Vim command character.  "Lang-Arg"
  210. isn't really another mode, it's just used here for this situation.
  211.    The simplest way to load a set of related language mappings is by using the
  212. 'keymap' option.  See |45.5|.
  213.    In Insert mode and in Command-line mode the mappings can be disabled with
  214. the CTRL-^ command |i_CTRL-^| |c_CTRL-^|.  When starting to enter a normal
  215. command line (not a search pattern) the mappings are disabled until a CTRL-^
  216. is typed.  The state last used is remembered for Insert mode and Search
  217. patterns separately.  The state for Insert mode is also used when typing a
  218. character as an argument to command like "f" or "t".
  219.    When adding a ":lmap" mapping the use of these mappings in Insert mode and
  220. for Search patterns will be switched on.
  221.    Language mappings will never be applied to already mapped characters.  They
  222. are only used for typed characters.  This assumes that the language mapping
  223. was already done when typing the mapping.
  224.  
  225.                             *map-multibyte*
  226. It is possible to map multibyte characters, but only the whole character.  You
  227. cannot map the first byte only.  This was done to prevent problems in this
  228. scenario: >
  229.     :set encoding=latin1
  230.     :imap <M-C> foo
  231.     :set encoding=utf-8
  232. The mapping for <M-C> is defined with the latin1 encoding, resulting in a 0xc3
  233. byte.  If you type the character á (0xea <M-a>) in UTF-8 encoding this is the
  234. two bytes 0xc3 0xa1.  You don't want the 0xc3 byte to be mapped then,
  235. otherwise it would be impossible to type the á character.
  236.  
  237.                             *map-listing*
  238. When listing mappings the characters in the first two columns are:
  239.  
  240.       CHAR    MODE    ~
  241.      <Space>    Normal, Visual and Operator-pending
  242.     n    Normal
  243.     v    Visual
  244.     o    Operator-pending
  245.     !    Insert and Command-line
  246.     i    Insert
  247.     l    ":lmap" mappings for Insert, Command-line and Lang-Arg
  248.     c    Command-line
  249.  
  250. Just before the {rhs} a special character can appear:
  251.     *    indicates that it is not remappable
  252.     &    indicates that only script-local mappings are remappable
  253.     @    indicates a buffer-local mapping
  254.  
  255. Everything from the first non-blank after {lhs} up to the end of the line
  256. (or '|') is considered to be part of {rhs}.  This allows the {rhs} to end
  257. with a space.
  258.  
  259. Note: When using mappings for Visual mode, you can use the "'<" mark, which
  260. is the start of the last selected Visual area in the current buffer |'<|.
  261.  
  262.                             *map_backslash*
  263. Note that only CTRL-V is mentioned here as a special character for mappings
  264. and abbreviations.  When 'cpoptions' does not contain 'B', a backslash can
  265. also be used like CTRL-V.  The <> notation can be fully used then |<>|.  But
  266. you cannot use "<C-V>" like CTRL-V to escape the special meaning of what
  267. follows.
  268.  
  269. To map a backslash, or use a backslash literally in the {rhs}, the special
  270. sequence "<Bslash>" can be used.  This avoids the need to double backslashes
  271. when using nested mappings.
  272.  
  273.                             *map-ambiguous*
  274. When two mappings start with the same sequence of characters, they are
  275. ambiguous.  Example: >
  276.     :imap aa foo
  277.     :imap aaa bar
  278. When Vim has read "aa", it will need to get another character to be able to
  279. decide if "aa" or "aaa" should be mapped.  This means that after typing "aa"
  280. that mapping won't get expanded yet, Vim is waiting for another character.
  281. If you type a space, then "foo" will get inserted, plus the space.  If you
  282. type "a", then "bar" will get inserted.
  283. {Vi does not allow ambiguous mappings}
  284.  
  285.                             *map_CTRL_C*
  286. It's not possible to use a CTRL-C in the {lhs}.  You just can't map CTRL-C.
  287. The reason is that CTRL-C must always be available to break a running command.
  288. Exception: When using the GUI version on MS-Windows CTRL-C can be mapped to
  289. allow a Copy command to the clipboard.  Use CTRL-Break to interrupt Vim.
  290.  
  291.                             *map_space_in_lhs*
  292. To include a space in {lhs} precede it with a CTRL-V (type two CTRL-Vs for
  293. each space).
  294.                             *map_space_in_rhs*
  295. If you want a {rhs} that starts with a space, use "<Space>".  To be fully Vi
  296. compatible (but unreadable) don't use the |<>| notation, precede {rhs} with a
  297. single CTRL-V (you have to type CTRL-V two times).
  298.                             *map_empty_rhs*
  299. You can create an empty {rhs} by typing nothing after a single CTRL-V (you
  300. have to type CTRL-V two times).  Unfortunately, you cannot do this in a vimrc
  301. file.
  302.                             *<Nop>*
  303. A easier way to get a mapping that doesn't produce anything, is to use "<Nop>"
  304. for the {rhs}.  This only works when the |<>| notation is enabled.  For
  305. example, to make sure that function key 8 does nothing at all: >
  306.     :map  <F8>  <Nop>
  307.     :map! <F8>  <Nop>
  308. <
  309.                     *<Leader>* *mapleader*
  310. To define a mapping which uses the "mapleader" variable, the special string
  311. "<Leader>" can be used.  It is replaced with the string value of "mapleader".
  312. If "mapleader" is not set or empty, a backslash is used instead.  Example: >
  313.     :map <Leader>A  oanother line<Esc>
  314. Works like: >
  315.     :map \A  oanother line<Esc>
  316. But after: >
  317.     :let mapleader = ","
  318. It works like: >
  319.     :map ,A  oanother line<Esc>
  320.  
  321. Note that the value of "mapleader" is used at the moment the mapping is
  322. defined.  Changing "mapleader" after that has no effect for already defined
  323. mappings.
  324.  
  325.                     *<LocalLeader>* *maplocalleader*
  326. Just like <Leader>, except that it uses "maplocalleader" instead of
  327. "mapleader".  <LocalLeader> is to be used for mappings which are local to a
  328. buffer.  Example: >
  329.       :map <LocalLeader>q  \DoItNow
  330. <
  331. In a global plugin <Leader> should be used and in a filetype plugin
  332. <LocalLeader>.  "mapleader" and "maplocalleader" can be equal.  Although, if
  333. you make them different, there is a smaller chance of mappings from global
  334. plugins to clash with mappings for filetype plugins.  For example, you could
  335. keep "mapleader" at the default backslash, and set "maplocalleader" to an
  336. underscore.
  337.  
  338.                             *map-<SID>*
  339. In a script the special key name "<SID>" can be used to define a mapping
  340. that's local to the script.  See |<SID>| for details.
  341.  
  342.                             *<Plug>*
  343. The special key name "<Plug>" can be used for an internal mapping, which is
  344. not to be matched with any key sequence.  This is useful in plugins
  345. |write-plugin|.
  346.  
  347.                             *<Char>* *<Char->*
  348. To map a character by its decimal, octal or hexadecimal number the <Char>
  349. construct can be used:
  350.     <Char-123>    character 123
  351.     <Char-033>    character 27
  352.     <Char-0x7f>    character 127
  353. This is useful to specify a (multi-byte) character in a 'keymap' file.
  354. Upper and lowercase differences are ignored.
  355.  
  356.                             *map-comments*
  357. It is not possible to put a comment after these commands, because the '"'
  358. character is considered to be part of the {lhs} or {rhs}.
  359.  
  360.                             *map_bar*
  361. Since the '|' character is used to separate a map command from the next
  362. command, you will have to do something special to include  a '|' in {rhs}.
  363. There are three methods:
  364.    use         works when               example    ~
  365.    <Bar>     '<' is not in 'cpoptions'       :map _l :!ls <Bar> more^M
  366.    \|         'b' is not in 'cpoptions'       :map _l :!ls \| more^M
  367.    ^V|         always, in Vim and Vi       :map _l :!ls ^V| more^M
  368.  
  369. (here ^V stands for CTRL-V; to get one CTRL-V you have to type it twice; you
  370. cannot use the <> notation "<C-V>" here).
  371.  
  372. All three work when you use the default setting for 'cpoptions'.
  373.  
  374. When 'b' is present in 'cpoptions', "\|" will be recognized as a mapping
  375. ending in a '\' and then another command.  This is Vi compatible, but
  376. illogical when compared to other commands.
  377.  
  378.                             *map_return*
  379. When you have a mapping that contains an Ex command, you need to put a line
  380. terminator after it to have it executed.  The use of <CR> is recommended for
  381. this (see |<>|).  Example: >
  382.    :map  _ls  :!ls -l %<CR>:echo "the end"<CR>
  383.  
  384. To avoid mapping of the characters you type in insert or Command-line mode,
  385. type a CTRL-V first.  The mapping in Insert mode is disabled if the 'paste'
  386. option is on.
  387.  
  388. Note that when an error is encountered (that causes an error message) the rest
  389. of the mapping is not executed.  This is Vi-compatible.
  390.  
  391. Note that the second character (argument) of the commands @zZtTfF[]rm'`"v
  392. and CTRL-X is not mapped.  This was done to be able to use all the named
  393. registers and marks, even when the command with the same name has been
  394. mapped.
  395.  
  396.                             *map-which-keys*
  397. If you are going to map something, you will need to choose which key(s) to use
  398. for the {lhs}.  You will have to avoid keys that are used for Vim commands,
  399. otherwise you would not be able to use those commands anymore.  Here are a few
  400. suggestions:
  401. - Function keys <F2>, <F3>, etc..  Also the shifted function keys <S-F1>,
  402.   <S-F2>, etc.  Note that <F1> is already used for the help command.
  403. - Meta-keys (with the ALT key pressed).
  404. - Use the '_' or ',' character and then any other character.  The "_" and ","
  405.   commands do exist in Vim (see |_| and |,|), but you probably never use them.
  406. - Use a key that is a synonym for another command.  For example: CTRL-P and
  407.   CTRL-N.  Use an extra character to allow more mappings.
  408.  
  409. See the file "index" for keys that are not used and thus can be mapped without
  410. losing any builtin function.  You can also use ":help {key}^D" to find out if
  411. a key is used for some command.  ({key} is the specific key you want to find
  412. out about, ^D is CTRL-D).
  413.  
  414.                             *map-examples*
  415. A few examples (given as you type them, for "<CR>" you type four characters;
  416. the '<' flag must not be present in 'cpoptions' for this to work). >
  417.  
  418.    :map <F3>  o#include
  419.    :map <M-g> /foo<CR>cwbar<Esc>
  420.    :map _x    d/END/e<CR>
  421.    :map! qq   quadrillion questions
  422. <
  423.                             *map-typing*
  424. Vim will compare what you type with the start of a mapped sequence.  If there
  425. is an incomplete match, it will get more characters until there either is a
  426. complete match or until there is no match at all.  Example: If you map! "qq",
  427. the first 'q' will not appear on the screen until you type another
  428. character.  This is because Vim cannot know if the next character will be a
  429. 'q' or not.  If the 'timeout' option is on (which is the default) Vim will
  430. only wait for one second (or as long as specified with the 'timeoutlen'
  431. option).  After that it assumes that the 'q' is to be interpreted as such.  If
  432. you type slowly, or your system is slow, reset the 'timeout' option.  Then you
  433. might want to set the 'ttimeout' option.
  434.  
  435.                             *map-keys-fails*
  436. There is one situation where key codes might not be recognized:
  437. - Vim can only read part of the key code.  Mostly this is only the first
  438.   character.  This happens on some Unix versions in an xterm.
  439. - The key code is after character(s) that are mapped.  E.g., "<F1><F1>" or
  440.   "g<F1>".
  441. The result is that the key code is not recognized in this situation, and the
  442. mapping fails.
  443. There are two actions needed to avoid this problem:
  444. - Remove the 'K' flag from 'cpoptions'.  This will make Vim wait for the rest
  445.   of the characters of the function key.
  446. - When using <F1> to <F4> the actual key code generated may correspond to
  447.   <xF1> to <xF4>.  There are mappings from <xF1> to <F1>, <xF2> to <F2>, etc.,
  448.   but these are not recognized after another half a mapping.  Make sure the
  449.   key codes for <F1> to <F4> are correct: >
  450.     :set <F1>=<type CTRL-V><type F1>
  451. < Type the <F1> as four characters.  The part after the "=" must be done with
  452.   the actual keys, not the literal text.
  453. Another solution is to use the actual key code in the mapping for the second
  454. special key: >
  455.     :map <F1><Esc>OP :echo "yes"<CR>
  456. Don't type a real <Esc>, Vim will recognize the key code and replace it with
  457. <F1> anyway.
  458.  
  459.                         *recursive_mapping*
  460. If you include the {lhs} in the {rhs} you have a recursive mapping.  When
  461. {lhs} is typed, it will be replaced with {rhs}.  When the {lhs} which is
  462. included in {rhs} is encountered it will be replaced with {rhs}, and so on.
  463. This makes it possible to repeat a command an infinite number of times.  The
  464. only problem is that the only way to stop this is by causing an error.  The
  465. macros to solve a maze uses this, look there for an example.  There is one
  466. exception: If the {rhs} starts with {lhs}, the first character is not mapped
  467. again (this is Vi compatible).
  468. For example: >
  469.    :map ab abcd
  470. will execute the "a" command and insert "bcd" in the text.  The "ab" in the
  471. {rhs} will not be mapped again.
  472.  
  473. If you want to exchange the meaning of two keys you should use the :noremap
  474. command.  For example: >
  475.    :noremap k j
  476.    :noremap j k
  477. This will exchange the cursor up and down commands.
  478.  
  479. With the normal :map command, when the 'remap' option is on, mapping takes
  480. place until the text is found not to be a part of a {lhs}.  For example, if
  481. you use: >
  482.    :map x y
  483.    :map y x
  484. Vim will replace x with y, and then y with x, etc.  When this has happened
  485. 'maxmapdepth' times (default 1000), Vim will give the error message
  486. "recursive mapping".
  487.  
  488.                             *:map-undo*
  489. If you include an undo command inside a mapped sequence, this will bring the
  490. text back in the state before executing the macro.  This is compatible with
  491. the original Vi, as long as there is only one undo command in the mapped
  492. sequence (having two undo commands in a mapped sequence did not make sense
  493. in the original Vi, you would get back the text before the first undo).
  494.  
  495.                             *:map-special-keys*
  496. There are three ways to map a special key:
  497. 1. The Vi-compatible method: Map the key code.  Often this is a sequence that
  498.    starts with <Esc>.  To enter a mapping like this you type ":map " and then
  499.    you have to type CTRL-V before hitting the function key.  Note that when
  500.    the key code for the key is in the termcap (the t_ options), it will
  501.    automatically be translated into the internal code and become the second
  502.    way of mapping (unless the 'k' flag is included in 'cpoptions').
  503. 2. The second method is to use the internal code for the function key.  To
  504.    enter such a mapping type CTRL-K and then hit the function key, or use
  505.    the form "#1", "#2", .. "#9", "#0", "<Up>", "<S-Down>", "<S-F7>", etc.
  506.    (see table of keys |key-notation|, all keys from <Up> can be used).  The
  507.    first ten function keys can be defined in two ways: Just the number, like
  508.    "#2", and with "<F>", like "<F2>".  Both stand for function key 2.  "#0"
  509.    refers to function key 10, defined with option 't_f10', which may be
  510.    function key zero on some keyboards.  The <> form cannot be used when
  511.    'cpoptions' includes the '<' flag.
  512. 3. Use the termcap entry, with the form <t_xx>, where "xx" is the name of the
  513.    termcap entry.  Any string entry can be used.  For example: >
  514.      :map <t_F3> G
  515. <  Maps function key 13 to "G".  This does not work if 'cpoptions' includes
  516.    the '<' flag.
  517.  
  518. The advantage of the second and third method is that the mapping will work on
  519. different terminals without modification (the function key will be
  520. translated into the same internal code or the actual key code, no matter what
  521. terminal you are using.  The termcap must be correct for this to work, and you
  522. must use the same mappings).
  523.  
  524. DETAIL: Vim first checks if a sequence from the keyboard is mapped.  If it
  525. isn't the terminal key codes are tried (see |terminal-options|).  If a
  526. terminal code is found it is replaced with the internal code.  Then the check
  527. for a mapping is done again (so you can map an internal code to something
  528. else).  What is written into the script file depends on what is recognized.
  529. If the terminal key code was recognized as a mapping the key code itself is
  530. written to the script file.  If it was recognized as a terminal code the
  531. internal code is written to the script file.
  532.  
  533. ==============================================================================
  534. 2. Abbreviations            *abbreviations* *Abbreviations*
  535.  
  536. Abbreviations are used in Insert mode, Replace mode and Command-line mode.
  537. If you enter a word that is an abbreviation, it is replaced with the word it
  538. stands for.  This can be used to save typing for often used long words.  And
  539. you can use it to automatically correct obvious spelling errors.
  540. Examples:
  541.  
  542.     :iab ms MicroSoft
  543.     :iab tihs this
  544.  
  545. There are three types of abbreviations:
  546.  
  547. full-id      The "full-id" type consists entirely of keyword characters (letters
  548.       and characters from 'iskeyword' option).  This is the most common
  549.       abbreviation.
  550.  
  551.       Examples: "foo", "g3", "-1"
  552.  
  553. end-id      The "end-id" type ends in a keyword character, but all the other
  554.       characters are not keyword characters.
  555.  
  556.       Examples: "#i", "..f", "$/7"
  557.  
  558. non-id      The "non-id" type ends in a non-keyword character, the other
  559.       characters may be of any type, excluding space and Tab.  {this type
  560.       is not supported by Vi}
  561.  
  562.       Examples: "def#", "4/7$"
  563.  
  564. Examples of strings that cannot be abbreviations: "a.b", "#def", "a b", "_$r"
  565.  
  566. An abbreviation is only recognized when you type a non-keyword character.
  567. This can also be the <Esc> that ends insert mode or the <CR> that ends a
  568. command.  The non-keyword character which ends the abbreviation is inserted
  569. after the expanded abbreviation.  An exception to this is the character <C-]>,
  570. which is used to expand an abbreviation without inserting any extra
  571. characters.
  572.  
  573. Example: >
  574.    :ab hh    hello
  575. <        "hh<Space>" is expanded to "hello<Space>"
  576.         "hh<C-]>" is expanded to "hello"
  577.  
  578. The characters before the cursor must match the abbreviation.  Each type has
  579. an additional rule:
  580.  
  581. full-id      In front of the match is a non-keyword character, or this is where
  582.       the line or insertion starts.  Exception: When the abbreviation is
  583.       only one character, it is not recognized if there is a non-keyword
  584.       character in front of it, other than a space or a <Tab>.
  585.  
  586. end-id      In front of the match is a keyword character, or a space or a <Tab>,
  587.       or this is where the line or insertion starts.
  588.  
  589. non-id      In front of the match is a space, <Tab> or the start of the line or
  590.       the insertion.
  591.  
  592. Examples: ({CURSOR} is where you type a non-keyword character) >
  593.    :ab foo   four old otters
  594. <        " foo{CURSOR}"      is expanded to " four old otters"
  595.         " foobar{CURSOR}" is not expanded
  596.         "barfoo{CURSOR}"  is not expanded
  597. >
  598.    :ab #i #include
  599. <        "#i{CURSOR}"      is expanded to "#include"
  600.         ">#i{CURSOR}"      is not expanded
  601. >
  602.    :ab ;; <endofline>"
  603. <        "test;;"      is not expanded
  604.         "test ;;"      is expanded to "test <endofline>"
  605.  
  606. To avoid the abbreviation in insert mode: Type part of the abbreviation, exit
  607. insert mode with <Esc>, re-enter insert mode with "a" and type the rest.  Or
  608. type CTRL-V before the character after the abbreviation.
  609. To avoid the abbreviation in Command-line mode: Type CTRL-V twice somewhere in
  610. the abbreviation to avoid it to be replaced.  A CTRL-V in front of a normal
  611. character is mostly ignored otherwise.
  612.  
  613. It is possible to move the cursor after an abbreviation: >
  614.    :iab if if ()<Left>
  615. This does not work if 'cpoptions' includes the '<' flag. |<>|
  616.  
  617. There are no default abbreviations.
  618.  
  619. Abbreviations are never recursive.  You can use ":ab f f-o-o" without any
  620. problem.  But abbreviations can be mapped.  {some versions of Vi support
  621. recursive abbreviations, for no apparent reason}
  622.  
  623. Abbreviations are disabled if the 'paste' option is on.
  624.  
  625.                 *:abbreviate-local* *:abbreviate-<buffer>*
  626. Just like mappings, abbreviations can be local to a buffer.  This is mostly
  627. used in a |filetype-plugin| file.  Example for a C plugin file: >
  628.     :abb <buffer> FF  for (i = 0; i < ; ++i)
  629. <
  630.                         *:ab* *:abbreviate*
  631. :ab[breviate]        list all abbreviations.  The character in the first
  632.             column indicates the mode where the abbreviation is
  633.             used: 'i' for insert mode, 'c' for Command-line
  634.             mode, '!' for both.
  635.  
  636. :ab[breviate] {lhs}    list the abbreviations that start with {lhs}
  637.  
  638. :ab[breviate] {lhs} {rhs}
  639.             add abbreviation for {lhs} to {rhs}.  If {lhs} already
  640.             existed it is replaced with the new {rhs}.  {rhs} may
  641.             contain spaces.
  642.  
  643.                         *:una* *:unabbreviate*
  644. :una[bbreviate] {lhs}    remove abbreviation for {lhs} from the list
  645.  
  646.                         *:norea* *:noreabbrev*
  647. :norea[bbrev] [lhs] [rhs]
  648.             same as ":ab", but no remapping for this {rhs} {not
  649.             in Vi}
  650.  
  651.                         *:ca* *:cabbrev*
  652. :ca[bbrev] [lhs] [rhs]    same as ":ab", but for Command-line mode only.  {not
  653.             in Vi}
  654.  
  655.                         *:cuna* *:cunabbrev*
  656. :cuna[bbrev] {lhs}    same as ":una", but for Command-line mode only.  {not
  657.             in Vi}
  658.  
  659.                         *:cnorea* *:cnoreabbrev*
  660. :cnorea[bbrev] [lhs] [rhs]
  661.             same as ":ab", but for Command-line mode only and no
  662.             remapping for this {rhs} {not in Vi}
  663.  
  664.                         *:ia* *:iabbrev*
  665. :ia[bbrev] [lhs] [rhs]    same as ":ab", but for Insert mode only.  {not in Vi}
  666.  
  667.                         *:iuna* *:iunabbrev*
  668. :iuna[bbrev] {lhs}    same as ":una", but for insert mode only.  {not in
  669.             Vi}
  670.  
  671.                         *:inorea* *:inoreabbrev*
  672. :inorea[bbrev] [lhs] [rhs]
  673.             same as ":ab", but for Insert mode only and no
  674.             remapping for this {rhs} {not in Vi}
  675.  
  676.                             *:abc* *:abclear*
  677. :abc[lear]        Remove all abbreviations.  {not in Vi}
  678.  
  679.                             *:iabc* *:iabclear*
  680. :iabc[lear]        Remove all abbreviations for Insert mode.  {not in Vi}
  681.  
  682.                             *:cabc* *:cabclear*
  683. :cabc[lear]        Remove all abbreviations for Command-line mode.  {not
  684.             in Vi}
  685.  
  686.                             *using_CTRL-V*
  687. It is possible to use special characters in the rhs of an abbreviation.
  688. CTRL-V has to be used to avoid the special meaning of most non printable
  689. characters.  How many CTRL-Vs need to be typed depends on how you enter the
  690. abbreviation.  This also applies to mappings.  Let's use an example here.
  691.  
  692. Suppose you want to abbreviate "esc" to enter an <Esc> character.  When you
  693. type the ":ab" command in Vim, you have to enter this: (here ^V is a CTRL-V
  694. and ^[ is <Esc>)
  695.  
  696. You type:   ab esc ^V^V^V^V^V^[
  697.  
  698.     All keyboard input is subjected to ^V quote interpretation, so
  699.     the first, third, and fifth ^V  characters simply allow the second,
  700.     and fourth ^Vs, and the ^[, to be entered into the command-line.
  701.  
  702. You see:    ab esc ^V^V^[
  703.  
  704.     The command-line contains two actual ^Vs before the ^[.  This is
  705.     how it should appear in your .exrc file, if you choose to go that
  706.     route.  The first ^V is there to quote the second ^V; the :ab
  707.     command uses ^V as its own quote character, so you can include quoted
  708.     whitespace or the | character in the abbreviation.   The :ab command
  709.     doesn't do anything special with the ^[ character, so it doesn't need
  710.     to be quoted.  (Although quoting isn't harmful; that's why typing 7
  711.     [but not 8!] ^Vs works.)
  712.  
  713. Stored as:  esc     ^V^[
  714.  
  715.     After parsing, the abbreviation's short form ("esc") and long form
  716.     (the two characters "^V^[") are stored in the abbreviation table.
  717.     If you give the :ab command with no arguments, this is how the
  718.     abbreviation will be displayed.
  719.  
  720.     Later, when the abbreviation is expanded because the user typed in
  721.     the word "esc", the long form is subjected to the same type of
  722.     ^V interpretation as keyboard input.  So the ^V protects the ^[
  723.     character from being interpreted as the "exit input-mode" character.
  724.     Instead, the ^[ is inserted into the text.
  725.  
  726. Expands to: ^[
  727.  
  728. [example given by Steve Kirkendall]
  729.  
  730. ==============================================================================
  731. 3. Local mappings and functions                *script-local*
  732.  
  733. When using several Vim script files, there is the danger that mappings and
  734. functions used in one script use the same name as in other scripts.  To avoid
  735. this, they can be made local to the script.
  736.  
  737.                         *<SID>* *<SNR>* *E81*
  738. The string "<SID>" can be used in a mapping or menu.  This requires that the
  739. '<' flag is not present in 'cpoptions'.
  740.    When executing the map command, Vim will replace "<SID>" with the special
  741. key code <SNR>, followed by a number that's unique for the script, and an
  742. underscore.  Example: >
  743.     :map <SID>Add
  744. could define a mapping "<SNR>23_Add".
  745.  
  746. When defining a function in a script, "s:" can be prepended to the name to
  747. make it local to the script.  But when a mapping is executed from outside of
  748. the script, it doesn't know in which script the function was defined.  To
  749. avoid this problem, use "<SID>" instead of "s:".  The same translation is done
  750. as for mappings.  This makes it possible to define a call to the function in
  751. mapping.
  752.  
  753. When a local function is executed, it runs in the context of the script it was
  754. defined in.  This means that new functions and mappings it defines can also
  755. use "s:" or "<SID>" and it will use the same unique number as when the
  756. function itself was defined.  Also, the "s:var" local script variables can be
  757. used.
  758.  
  759. When executing an autocommand or a user command, it will run in the context of
  760. the script it was defined in.  This makes it possible that the command calls a
  761. local function or uses a local mapping.
  762.  
  763. Otherwise, using "<SID>" outside of a script context is an error.
  764.  
  765. If you need to get the script number to use in a complicated script, you can
  766. use this trick: >
  767.     :map <SID>xx <SID>xx
  768.     :let s:sid = maparg("<SID>xx")
  769.     :unmap <SID>xx
  770. And remove the trailing "xx".
  771.  
  772. The "<SNR>" will be shown when listing functions and mappings.  This is useful
  773. to find out what they are defined to.
  774.  
  775. The |:scriptnames| command can be used to see which scripts have been sourced
  776. and what their <SNR> number is.
  777.  
  778. This is all {not in Vi} and {not available when compiled without the +eval
  779. feature}.
  780.  
  781. ==============================================================================
  782. 4. User-defined commands                *user-commands*
  783.  
  784. It is possible to define your own Ex commands. A user-defined command can act
  785. just like a built-in command (it can have a range or arguments, arguments can
  786. be completed as filenames or buffer names, etc), except that when the command
  787. is executed, it is transformed into a normal ex command and then executed.
  788.  
  789. For starters: See section |40.2| in the user manual.
  790.  
  791.                             *E183*
  792. All user defined commands must start with an uppercase letter, to avoid
  793. confusion with builtin commands. (There are a few builtin commands, notably
  794. :Next, :Print and :X, which do start with an uppercase letter.  The builtin
  795. will always take precedence in these cases).  The other characters of the user
  796. command can be uppercase letters, lowercase letters or digits.  When using
  797. digits, note that other commands that take a numeric argument may become
  798. ambiguous.  For example, the command ":Cc2" could be the user command ":Cc2"
  799. without an argument, or the command ":Cc" with argument "2".  It is advised to
  800. put a space between the command name and the argument to avoid these problems.
  801.  
  802. When using a user-defined command, the command can be abbreviated. However, if
  803. an abbreviation is not unique, an error will be issued. Furthermore, a
  804. built-in command will always take precedence.
  805.  
  806. Example: >
  807.     :command Rename ...
  808.     :command Renumber ...
  809.     :Rena                " Means "Rename"
  810.     :Renu                " Means "Renumber"
  811.     :Ren                " Error - ambiguous
  812.     :command Paste ...
  813.     :P                " The built-in :Print
  814.  
  815. It is recommended that full names for user-defined commands are used in
  816. scripts.
  817.  
  818. :com[mand]                        *:com* *:command*
  819.             List all user-defined commands. When listing commands,
  820.             the characters in the first two columns are
  821.                 !    Command has the -bang attribute
  822.                 "    Command has the -register attribute
  823.                 b    Command is local to current buffer
  824.             (see below for details on attributes)
  825.  
  826. :com[mand] {cmd}    List the user-defined commands that start with {cmd}
  827.  
  828.                             *E174* *E182*
  829. :com[mand][!] [{attr}...] {cmd} {rep}
  830.             Define a user command.  The name of the command is
  831.             {cmd} and its replacement text is {rep}. The command's
  832.             attributes (see below) are {attr}. If the command
  833.             already exists, an error is reported, unless a ! is
  834.             specified, in which case the command is redefined.
  835.  
  836. :delc[ommand] {cmd}                *:delc* *:delcommand* *E184*
  837.             Delete the user-defined command {cmd}.
  838.  
  839. :comc[lear]                        *:comc* *:comclear*
  840.             Delete all user-defined commands.
  841.  
  842. Command attributes
  843.  
  844. User-defined commands are treated by Vim just like any other ex commands. They
  845. can have arguments, or have a range specified. Arguments are subject to
  846. completion as filenames, buffers, etc. Exactly how this works depends upon the
  847. command's attributes, which are specified when the command is defined.
  848.  
  849. There are a number of attributes, split into four categories: argument
  850. handling, completion behavior, range handling, and special cases. The
  851. attributes are described below, by category.
  852.  
  853. Argument handling                    *E175* *E176*
  854.  
  855. By default, a user defined command will take no arguments (and an error is
  856. reported if any are supplied). However, it is possible to specify that the
  857. command can take arguments, using the -nargs attribute. Valid cases are:
  858.  
  859.     -nargs=0    No arguments are allowed (the default)
  860.     -nargs=1    Exactly one argument is required
  861.     -nargs=*    Any number of arguments are allowed (0, 1, or many)
  862.     -nargs=?    0 or 1 arguments are allowed
  863.     -nargs=+    Arguments must be supplied, but any number are allowed
  864.  
  865. Arguments are considered to be separated by (unescaped) spaces or Tabs in this
  866. context.
  867.  
  868. Completion behavior                *:command-completion*
  869.                             *E179* *E180* *E181*
  870. By default, the arguments of user defined commands do not undergo completion.
  871. However, by specifying one or the other of the following attributes, argument
  872. completion can be enabled:
  873.  
  874.     -complete=augroup    autocmd groups
  875.     -complete=buffer    buffer names
  876.     -complete=command    Ex command (and arguments)
  877.     -complete=dir        directory names
  878.     -complete=environment    environment variable names
  879.     -complete=event        autocommand events
  880.     -complete=expression    Vim expression
  881.     -complete=file        file and directory names
  882.     -complete=function    function name
  883.     -complete=help        help subjects
  884.     -complete=highlight    highlight groups
  885.     -complete=mapping    mapping name
  886.     -complete=menu        menus
  887.     -complete=option    options
  888.     -complete=tag        tags
  889.     -complete=tag_listfiles    tags, file names are shown when CTRL-D is hit
  890.     -complete=var        user variables
  891.  
  892. Range handling                        *E177* *E178*
  893.  
  894. By default, user-defined commands do not accept a line number range. However,
  895. it is possible to specify that the command does take a range (the -range
  896. attribute), or that it takes an arbitrary count value, either in the line
  897. number position (-range=N, like the |:split| command) or as a "count"
  898. argument (-count=N, like the |:Next| command). Possible attributes are:
  899.  
  900.     -range        Range allowed, default is current line
  901.     -range=%    Range allowed, default is whole file (1,$)
  902.     -range=N    A count (default N) which is specified in the line
  903.             number position (like |:split|)
  904.     -count=N    A count (default N) which is specified either in the line
  905.             number position, or as an initial argument (like |:Next|)
  906.             Specifying -count (without a default) acts like -count=0
  907.  
  908. Note that -range=N and -count=N are mutually exclusive - only one should be
  909. specified.
  910.  
  911. Special cases
  912.  
  913. There are some special cases as well:
  914.  
  915.     -bang        The command can take a ! modifier (like :q or :w)
  916.     -bar        The command can be followed by a "|" and another command.
  917.             A "|" inside the command argument is not allowed then.
  918.             Also checks for a " to start a comment.
  919.     -register   The first argument to the command can be an optional
  920.             register name (like :del, :put, :yank).
  921.     -buffer        The command will only be available in the current buffer.
  922.  
  923. In the cases of the -count and -register attributes, if the optional argument
  924. is supplied, it is removed from the argument list and is available to the
  925. replacement text separately.
  926.  
  927. Replacement text
  928.  
  929. The replacement text for a user defined command is scanned for special escape
  930. sequences, using <...> notation. Escape sequences are replaced with values
  931. from the entered command line, and all other text is copied unchanged. The
  932. resulting string is executed as an Ex command. If the initial < of an escape
  933. sequence is preceded by a backslash, the sequence is copied unchanged.
  934.  
  935. The valid escape sequences are
  936.  
  937.                         *<line1>*
  938.     <line1>    The starting line of the command range.
  939.                         *<line2>*
  940.     <line2>    The final line of the command range.
  941.                         *<count>*
  942.     <count>    Any count supplied (as described for the '-range'
  943.         and '-count' attributes).
  944.                         *<bang>*
  945.     <bang>    (See the '-bang' attribute) Expands to a ! if the
  946.         command was executed with a ! modifier, otherwise
  947.         expands to nothing.
  948.                         *<reg>* *<register>*
  949.     <reg>    (See the '-register' attribute) The optional register,
  950.         if specified. Otherwise, expands to nothing. <register>
  951.         is a synonym for this.
  952.                         *<args>*
  953.     <args>    The command arguments, exactly as supplied (but as
  954.         noted above, any count or register can consume some
  955.         of the arguments, which are then not part of <args>).
  956.     <lt>    A single '<' (Less-Than) character.  This is needed if you
  957.         want to get a literal copy of one of these escape sequences
  958.         into the expansion - for example, to get <bang>, use
  959.         <lt>bang>.
  960.  
  961. If the first two characters of an escape sequence are "q-" (for example,
  962. <q-args>) then the value is quoted in such a way as to make it a valid value
  963. for use in an expression.  This uses the argument as one single value.
  964.  
  965. To allow commands to pass their arguments on to a user-defined function, there
  966. is a special form <f-args> ("function args"). This splits the command
  967. arguments at spaces and Tabs, quotes each argument individually, and the
  968. <f-args> sequence is replaced by the comma-separated list of quoted arguments.
  969. See the Mycmd example below.  When there is no argument, <f-args> also has no
  970. argument.
  971.  
  972. Examples >
  973.  
  974.    " Delete everything after here to the end
  975.    :com Ddel +,$d
  976.  
  977.    " Rename the current buffer
  978.    :com -nargs=1 -bang -complete=file Ren f <args>|w<bang>
  979.  
  980.    " Replace a range with the contents of a file
  981.    " (Enter this all as one line)
  982.    :com -range -nargs=1 -complete=file
  983.      Replace <line1>-pu_|<line1>,<line2>d|r <args>|<line1>d
  984.  
  985.    " Count the number of lines in the range
  986.    :com! -range -nargs=0 Lines :echo <line2> - <line1> + 1 "lines"
  987.  
  988.    " Call a user function (example of <f-args>)
  989.    :com -nargs=* Mycmd call Myfunc(<f-args>)
  990.  
  991. When executed as: >
  992.     :Mycmd arg1 arg2
  993. This will invoke: >
  994.     :call Myfunc("arg1","arg2")
  995.  
  996.    :" A more substantial example
  997.    :function Allargs(command)
  998.    :    let i = 0
  999.    :    while i < argc()
  1000.    :       if filereadable(argv(i))
  1001.    :        execute "e " . argv(i)
  1002.    :         execute a:command
  1003.    :      endif
  1004.    :      let i = i + 1
  1005.    :   endwhile
  1006.    :endfunction
  1007.    :command -nargs=+ -complete=command Allargs call Allargs(<q-args>)
  1008.  
  1009. The command Allargs takes any Vim command(s) as argument and executes it on all
  1010. files in the argument list.  Usage example (note use of the "e" flag to ignore
  1011. errors and the "update" command to write modified buffers): >
  1012.     :Allargs %s/foo/bar/ge|update
  1013. This will invoke: >
  1014.     :call Allargs("%s/foo/bar/ge|update")
  1015. <
  1016. When defining an user command in a script, it will be able to call functions
  1017. local to the script and use mappings local to the script.  When the user
  1018. invokes the user command, it will run in the context of the script it was
  1019. defined in.  This matters if |<SID>| is used in a command.
  1020.  
  1021.  vim:tw=78:ts=8:ft=help:norl:
  1022.